/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package give.replay.config;

import give.util.FileTools;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.table.TableModel;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author koller
 */
public class ReplayConfiguration {

    private List<ColumnType> columnTypes;
    private List<String> columnDbFields;
    private List<String> columnLabels;
    private String dbtable = "games";
    private TableModel swingTableModel = null;

    public ReplayConfiguration() {
        columnTypes = new ArrayList<ColumnType>();
        columnDbFields = new ArrayList<String>();
        columnLabels = new ArrayList<String>();
    }

    public void readFromFile(String filename) {
        try {
            SAXParserFactory.newInstance().newSAXParser().parse(new InputSource(FileTools.openAsFileOrResource(filename, "/")), new ConfigurationFileHandler());
        } catch (Exception e) {
            System.err.println("Fatal error: An error occurred while parsing the replay configuration file.");
            e.printStackTrace(System.err);
            System.exit(1);
        }
    }

    public String getDbTableName() {
        return dbtable;
    }

    public class ConfigurationFileHandler extends DefaultHandler {

        @Override
        public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
            if (name.equals("dbSelectionColumns")) {
                if (attributes.getValue("table-name") != null) {
                    dbtable = attributes.getValue("table-name");
                }
            } else if (name.equals("dbSelectionColumn")) {
                columnDbFields.add(attributes.getValue("db"));
                columnLabels.add(attributes.getValue("label"));
                columnTypes.add(ColumnType.lookup(attributes.getValue("type")));
            }
        }
    }

    public Map<String, Object> convertResultSet(ResultSet rs) throws SQLException {
        Map<String, Object> ret = new HashMap<String, Object>();

        for (int i = 0; i < columnDbFields.size(); i++) {
            String field = columnDbFields.get(i);

            switch (columnTypes.get(i)) {
                case INT:
                    ret.put(field, new Integer(rs.getInt(field)));
                    break;
                case STRING:
                    ret.put(field, rs.getString(field));
                    break;
                case DATE_IN_MS:
                    ret.put(field, new Date(rs.getLong(field)));
                    break;
            }
        }

        return ret;
    }

    public List<String> getDbColumns() {
        return columnDbFields;
    }

    public TableModel getTableModel() {
        String[] labels = columnLabels.toArray(new String[0]);
        final Class[] classes = new Class[columnTypes.size()];
        final boolean[] cannotEdit = new boolean[columnTypes.size()];

        for (int i = 0; i < columnTypes.size(); i++) {
            classes[i] = columnTypes.get(i).swingDisplayClass;
            cannotEdit[i] = false;
        }

        return new javax.swing.table.DefaultTableModel(
                new Object[][]{},
                labels) {

            Class[] types = classes;
            boolean[] canEdit = cannotEdit;

            @Override
            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        };
    }
}
